import { createRouter, createWebHistory } from "vue-router";
import TaskListComponent from "@/views/Page/TaskListComponent.vue"; // 确保路径正确

const routes = [
  {
    path: "/",
    redirect: "/home/index",
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/Login.vue"),
    meta: {
      title: "登录",
    },
  },
  {
    path: "/home",
    name: "home",
    component: () => import("@/views/Home.vue"),
    children: [
      {
        path: "index",
        name: "index",
        component: () => import("@/views/Index.vue"),
        meta: {
          title: "首页",
          affix: true,
        },
      },
      {
        path: "memo",
        name: "memo",
        component: () => import("@/views/Memo.vue"),
        meta: {
          title: "备忘录",
        },
      },
      {
        path: "project",
        name: "project",
        meta: {
          title: "项目",
        },
        redirect: "/home/project/participate",
        children: [
          {
            path: "participate",
            name: "participate",
            component: () => import("@/views/Page/Participate.vue"),
            meta: {
              title: "我参与的",
            },
          },
          {
            path: "create",
            name: "create",
            component: () => import("@/views/Page/Create.vue"),
            meta: {
              title: "我创建的",
            },
          },
          {
            path: "participate/:projectName/:projectId",
            name: "participate/:projectName/:projectId",
            component: () => import("@/views/Page/TaskListComponent.vue"),
            props: true,
            meta: {
              title: "任务",
            },
          },
        ],
      },

      {
        path: "task",
        name: "task",
        meta: {
          title: "任务",
        },
        redirect: "/home/task/unfinished",
        children: [
          {
            path: "unfinished",
            name: "unfinished",
            component: () => import("@/views/Page/Unfinished.vue"),
            meta: {
              title: "待办",
            },
          },
          {
            path: "done",
            name: "done",
            component: () => import("@/views/Page/Done.vue"),
            meta: {
              title: "已完成",
            },
          },
        ],
      },

      {
        path: "menu-1",
        name: "menu-1",
        component: () => import("@/views/Menu1.vue"),
        meta: {
          title: "菜单1",
        },
      },
      {
        path: "menu-2",
        name: "menu-2",
        component: () => import("@/views/Menu2.vue"),
        meta: {
          title: "菜单2",
        },
      },
    ],
  },
  // {
  //     path: '/tasks/:projectId/:projectName',
  //     name: 'TaskList',
  //     component: TaskListComponent,
  //     props: true // 启用props传递
  //   }
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

//路由守卫
const whiteList = ["/login"];
router.beforeEach((to, from, next) => {
  document.title = to.meta.title;
  if (!whiteList.includes(to.path)) {
    // if (!localStorage.getItem("token")) {
    //   router.push("/login");
    //   return;
    // }
    next();
    return;
  }
  next();
});

export default router;
